package com.tarena.lbs.basic.web.controller;

import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.service.BusinessService;
import com.tarena.lbs.pojo.basic.bo.BusinessBO;
import com.tarena.lbs.pojo.basic.param.BusinessParam;
import com.tarena.lbs.pojo.basic.query.BusinessQuery;
import com.tarena.lbs.pojo.basic.vo.BusinessVO;
import com.tarena.lbs.pojo.basic.vo.DictItemVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    @GetMapping("/admin/basic/business/info/list")
    public Result<PageResult<BusinessVO>> getBusiness(BusinessQuery businessQuery)throws BusinessException{
        //业务层给我返回一个可以封装pageResult属性的对象 pageNo pageSize
        //业务返回的数据 total objects
        log.debug("C1-批量查询:{}",businessQuery);
        businessQuery.setPageNo(1);
        businessQuery.setPageSize(10);
        BasePageList<BusinessBO> boList=businessService.pageList(businessQuery);
        PageResult<BusinessVO> pageVOS=pageBOStoPageVOS(boList,businessQuery);
        return new Result<>(pageVOS);
    }

    /**
     * 查询商家行业分类
     */
    @GetMapping("/admin/basic/dictItem/info/getlistAll/{dictId}")
    public Result<List<DictItemVO>> businessTypes(){
        String values="[{\"id\":89,\"dictId\":34,\"label\":\"美食餐饮\",\"dictValue\":\"美食餐饮\",\"sort\":10,\"status\":0,\"remark\":\"美食餐饮\"}," +
                "{\"id\":90,\"dictId\":34,\"label\":\"旅行探险\",\"dictValue\":\"旅行探险\",\"sort\":10,\"status\":0,\"remark\":\"旅行探险\"}]";
        //使用工具类,把json字符串转换成对象
        List<DictItemVO> dictItemVOS = JSON.parseArray(values, DictItemVO.class);
        //手动创建对象封装数据
        /*
        List<DictItemVO>dictItemVOS=new ArrayList<>();
        DictItemVO dictItemVO=new DictItemVO();
        dictItemVO.setId(89);
        dictItemVO.setDictId(34);
        dictItemVO.setLabel("美食餐饮");
        dictItemVO.setDictValue("美食餐饮");
        dictItemVO.setSort(10);
        dictItemVO.setStatus(0);
        dictItemVO.setRemark("美食餐饮");
        dictItemVOS.add(dictItemVO);
        DictItemVO dictItemVO1=new DictItemVO();
        dictItemVO1.setId(90);
        dictItemVO1.setDictId(34);
        dictItemVO1.setLabel("旅行探险");
        dictItemVO1.setDictValue("旅行探险");
        dictItemVO1.setSort(10);
        dictItemVO1.setStatus(0);
        dictItemVO1.setRemark("旅行探险");
        dictItemVOS.add(dictItemVO1);
        */
        log.debug("行业类型数据:{}",dictItemVOS);
        return new Result<List<DictItemVO>>(dictItemVOS);
    }

    //新增商家
    @PostMapping("/basic/business/info/save")
    public Result<Void> businessSave(@RequestBody BusinessParam businessParam)throws BusinessException{
        log.debug("C2-新增商家:{}",businessParam);
        businessService.businessSave(businessParam);
        return Result.success();
    }

    @GetMapping("business/info/detail/{id}")
    public Result<BusinessVO> businessDetail(@PathVariable Integer id) throws BusinessException{
        BusinessBO bo=businessService.businessDetail(id);
        BusinessVO vo=new BusinessVO();
        if (bo!=null){
            BeanUtils.copyProperties(bo,vo);
        }
        return new Result<>(vo);
    }


    private PageResult<BusinessVO> pageBOStoPageVOS(
            BasePageList<BusinessBO> boList,
            BusinessQuery businessQuery) {
        PageResult<BusinessVO> pageVOS=new PageResult<>();
        pageVOS.setPageNo(businessQuery.getPageNo());
        pageVOS.setPageSize(businessQuery.getPageSize());
        pageVOS.setTotal(boList.getTotal());
        List<BusinessVO> voList=new ArrayList<>();
        List<BusinessBO> list = boList.getList();
        //加一个if判断 查询的分页数据非空
        if (boList.getTotal()!=0){
            for (BusinessBO businessBO : list) {
                BusinessVO vo=new BusinessVO();
                BeanUtils.copyProperties(businessBO,vo);
                voList.add(vo);
            }
        }
        pageVOS.setObjects(voList);
        return pageVOS;
    }
}
